package cn.wk.libs4a.view.phonecontact;

import java.util.ArrayList;
import java.util.List;

import android.os.AsyncTask;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.EditText;
import android.widget.ListView;
import cn.wk.libs4a.R;
import cn.wk.libs4a.WKBaseActivity;
import cn.wk.libs4a.view.WKABCSideBar;
import cn.wk.libs4a.view.WKProgressDlg;

public class WKPhoneContactAct extends WKBaseActivity {
	private EditText et_search;// 搜索框
	private ListView contacts_list;// 联系人显示列表
	private WKPhoneContactAdapter contactAdapter;
	private List<WKPhoneContactBean> contacts;// 手机通讯录联系人
	private WKABCSideBar indexbar;// 自定义字母索引列表
	private WKPhoneContactUtils utils;// 联系人工具类

	private void initView() {
		et_search = (EditText) this.findViewById(R.id.et_search);
		contacts_list = (ListView) this.findViewById(R.id.contacts_list);
		indexbar = (WKABCSideBar) this.findViewById(R.id.indexbar);
		contactAdapter = new WKPhoneContactAdapter(this);
		contacts_list.setAdapter(contactAdapter);
		indexbar.setListView(contacts_list);

		contacts_list.setOnScrollListener(new OnScrollListener() {
			public void onScrollStateChanged(AbsListView arg0, int arg1) {
				if (arg1 == OnScrollListener.SCROLL_STATE_IDLE) {
					app().hideInputKeyboard();
				}
			}

			public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {
			}
		});

		et_search.addTextChangedListener(new TextWatcher() {
			private String startText = null;// 变化前的文字
			private String endText = null;// 变化后的文字

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// 当文字发生变化时
				endText = s.toString();
				if (endText.equals(startText)) {// 文字没有发生变化
				} else {
					if (endText.startsWith(startText)) {// 在原有文字上添加文字
						setSearchContacts(endText, true);
					} else {// 不是添加
						setSearchContacts(endText, false);
					}
				}
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				startText = s.toString();
			}

			public void afterTextChanged(Editable s) {
			}
		});

		utils = new WKPhoneContactUtils(this);
	}

	private void initData() {
		contacts = utils.getContactsCache();
		// 如果第一次进入，显示进度条
		if (contacts == null || contacts.size() == 0) {
			WKProgressDlg.show(this, false);
		}
		new UpdateContactTask().execute();
		contactAdapter.setContacts(contacts);
		contactAdapter.notifyDataSetChanged();
	}

	/**
	 * 异步获取联系人
	 */
	class UpdateContactTask extends AsyncTask<Void, Integer, Integer> {

		protected void onPreExecute() {
		}

		protected Integer doInBackground(Void... params) {
			contacts = utils.getContacts();
			return null;
		}

		protected void onPostExecute(Integer integer) {
			WKProgressDlg.hide();
			contactAdapter.setContacts(contacts);
			contactAdapter.notifyDataSetChanged();
		}

		protected void onProgressUpdate(Integer... values) {
		}
	}

	/**
	 * 根据条件筛选
	 * 
	 * @param param
	 */
	public void setSearchContacts(String param, boolean isAppend) {
		List<WKPhoneContactBean> contactsShow = null;
		if (isAppend) {
			contactsShow = contactAdapter.getContacts();
		} else {
			contactsShow = contacts;
		}
		List<WKPhoneContactBean> contactsTemp = new ArrayList<WKPhoneContactBean>();// 根据条件搜索的联系人
		int workType = this.getWordType(param);
		indexbar.setVisibility(workType == 0 ? View.VISIBLE : View.INVISIBLE);
		if (workType == 0) {// 搜索条件为空
			contactsTemp = contactsShow;
		} else {
			// String pinyinParam = WKPhoneContactUtils.getPingYin(param)
			// .toUpperCase();
			WKPhoneContactBean bean;
			for (int i = 0; i < contactsShow.size(); i++) {
				bean = contactsShow.get(i);
				boolean flag1 = bean.getName() != null
						&& bean.getName().contains(param);
				boolean flag2 = bean.getPinyinName() != null
						&& bean.getPinyinName().contains(param);
				boolean flag3 = bean.getPhoneNo() != null
						&& bean.getPhoneNo().contains(param);

				// String name = contactsShow.get(i).getName();
				// String pinyinName = WKPhoneContactUtils.getPingYin(name)
				// .toUpperCase();
				// boolean flag1 = workType == 1 && name.contains(param);//
				// 1,汉字(非A-Z)开头
				// // 中文查询
				// boolean flag2 = workType == 2
				// && (pinyinName.startsWith(pinyinParam) || WKPhoneContactUtils
				// .getFirstSpell(name).toUpperCase()
				// .contains(param.toUpperCase()));// 2,A-Z开头且全为A-Z组成
				// // 拼音查询
				// boolean flag3 = workType == 3 && name.contains(param);//
				// 3,A-Z开头,中间包含非A-Z

				if (flag1 || flag2 || flag3) {
					contactsTemp.add(contactsShow.get(i));
				}
			}
		}
		contactAdapter.setContacts(contactsTemp);
		contactAdapter.notifyDataSetChanged();
	}

	/**
	 * 判断String的类型
	 * 
	 * @param word
	 * @return 0,为null 1,汉字(非A-Z)开头 2,A-Z开头且全为A-Z组成 3,A-Z开头,中间包含非A-Z
	 */
	private int getWordType(String word) {
		if (word == null || TextUtils.isEmpty(word.trim())) {
			return 0;
		}
		word = word.trim().toUpperCase();
		if (word.charAt(0) < 'A' || word.charAt(0) > 'Z') {
			return 1;
		}

		for (int i = 0; i < word.length(); i++) {
			if (word.charAt(i) < 'A' || word.charAt(i) > 'Z') {
				return 3;
			}
		}

		return 2;
	}

	@Override
	public void loadXml() {
		// getWindow().setSoftInputMode(
		// WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		setContentView(R.layout.wk_act_phone_contact);
	}

	@Override
	public void init() {
		initView();
	}

	@Override
	public void setData() {
		initData();
	}

}
